home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / games / mdlv13.zip / MDLSRC.ZIP / BG_GRX.CC next >
C/C++ Source or Header  |  1996-07-09  |  35KB  |  1,255 lines

  1. //
  2. // this file by brian martin 1996
  3. // brian@phyast.pitt.edu
  4. //
  5. // this is part of the source for the MedDLe quake model viewer/editor
  6. //
  7.  
  8. // NOTE: much of this file comes from the great 3d tutorial 3gdpl.
  9. // go get it and learn it.
  10. // other stuff might be from the vga trainers
  11. // some stuff is original
  12. // It  used to all be original, but I ran into some bugs so copied
  13. // and pasted from everywhere. That means the zbuffer stuff is gone,
  14. // but shaded texture mapping is here.
  15. //
  16.  
  17. #include <dpmi.h>
  18. #include<stdio.h>
  19. #include<stdlib.h>
  20. #include<string.h>
  21. #include<dos.h>
  22. #include<grdriver.h>
  23. //#include<grx20.h>
  24. #include"mdl.h"
  25. #include<math.h>
  26. #include<limits.h>
  27.  
  28. ////grx stuff
  29. GrFont *BG_Font;
  30. GrContext *w1;
  31. GrTextOption opt;
  32.  
  33. int G_start[1024];              /* polygon's */
  34. int G_end[1024];                /* horizontal boundaries */
  35. int G_miny,G_maxy;                          /* vertical boundaries */
  36.  
  37. #define G_P      10                         /* fixed point math prcision */
  38. #define G_LINEAR 32                         /* linearely interpolate for */
  39.  
  40. signed_32_bit G_0_start[1024];  /* contains [32-G_P].[G_P] values */
  41. signed_32_bit G_0_end[1024];    /* this thingie is to work faster */
  42. signed_32_bit G_1_start[1024];  /* then multidimensional array */
  43. signed_32_bit G_1_end[1024];    /* hope so, */
  44. signed_32_bit G_2_start[1024];
  45. signed_32_bit G_2_end[1024];
  46.  
  47. unsigned BG_ScreenWidth;
  48. unsigned BG_ScreenHeight;
  49. int BG_ClipXMin, BG_ClipYMin, BG_ClipXMax, BG_ClipYMax;       // clipping
  50. int BG_2DClipping;                          /* type of performed clipping */
  51.  
  52.  
  53. int BG_TempMinY,BG_TempMaxY;                          /* vertical boundaries */
  54.  
  55.  
  56. byte MDL_Font[354]=
  57. {
  58.  0x00,0x00,0x00,0x00,0x00,0x00, 0x30,0x30,0x30,0x00,0x30,0x00,
  59.  0xd8,0x90,0x00,0x00,0x00,0x00, 0x6c,0xfe,0x6c,0xfe,0x6c,0x00,
  60.  0x7e,0xd0,0x7c,0x16,0xfc,0x00, 0xcc,0x18,0x30,0x60,0xcc,0x00,
  61.  0x60,0x90,0x74,0xd8,0x6c,0x00, 0x30,0x20,0x00,0x00,0x00,0x00,
  62.  0x60,0xc0,0xc0,0xc0,0x60,0x00, 0x18,0x0c,0x0c,0x0c,0x18,0x00,
  63.  0x6c,0x38,0xfe,0x38,0x6c,0x00, 0x00,0x30,0xfc,0x30,0x00,0x00,
  64.  0x00,0x00,0x00,0x30,0x20,0x00, 0x00,0x00,0x7c,0x00,0x00,0x00,
  65.  0x00,0x00,0x00,0x00,0xc0,0x00, 0x0c,0x18,0x30,0x60,0xc0,0x00,
  66.  0x78,0x84,0x84,0x84,0x78,0x00, 0x04,0x0c,0x14,0x04,0x04,0x00,
  67.  0x78,0x84,0x18,0x20,0x7c,0x00, 0x78,0x04,0x38,0x04,0x78,0x00,
  68.  0x84,0x84,0xfc,0x04,0x04,0x00, 0xfc,0x80,0xf8,0x04,0xf8,0x00,
  69.  0x30,0x40,0xf8,0x84,0x78,0x00, 0xfc,0x04,0x08,0x10,0x10,0x00,
  70.  0x78,0x84,0x78,0x84,0x78,0x00, 0x78,0x84,0x7c,0x08,0x30,0x00,
  71.  0x00,0x30,0x00,0x30,0x00,0x00, 0x00,0x30,0x00,0x30,0x20,0x00,
  72.  0x30,0x60,0xc0,0x60,0x30,0x00, 0x00,0x7c,0x00,0x7c,0x00,0x00,
  73.  0x30,0x18,0x0c,0x18,0x30,0x00, 0x78,0xcc,0x18,0x00,0x18,0x00,
  74.  0x78,0xcc,0xdc,0xc0,0x7c,0x00, 0x30,0x48,0x84,0xfc,0x84,0x00,
  75.  0xf8,0x84,0xf8,0x84,0xf8,0x00, 0x7c,0x80,0x80,0x80,0x7c,0x00,
  76.  0xf8,0x84,0x84,0x84,0xf8,0x00, 0xfc,0x80,0xf0,0x80,0xfc,0x00,
  77.  0xfc,0x80,0xf0,0x80,0x80,0x00, 0x7c,0x80,0x84,0x84,0x7c,0x00,
  78.  0x84,0x84,0xfc,0x84,0x84,0x00, 0x10,0x10,0x10,0x10,0x10,0x00,
  79.  0x04,0x04,0x04,0x84,0x78,0x00, 0x84,0x88,0xf0,0x88,0x84,0x00,
  80.  0x80,0x80,0x80,0x80,0xfc,0x00, 0x82,0xc6,0xaa,0x92,0x82,0x00,
  81.  0x84,0xc4,0xa4,0x94,0x8c,0x00, 0x78,0x84,0x84,0x84,0x78,0x00,
  82.  0xf8,0x84,0xf8,0x80,0x80,0x00, 0x78,0x84,0x84,0x78,0x1c,0x00,
  83.  0xf8,0x84,0xf8,0x90,0x88,0x00, 0x7c,0x80,0x78,0x04,0xf8,0x00,
  84.  0xfe,0x10,0x10,0x10,0x10,0x00, 0x84,0x84,0x84,0x84,0x78,0x00,
  85.  0x84,0x84,0x84,0x48,0x30,0x00, 0x82,0x82,0x92,0xaa,0x44,0x00,
  86.  0x84,0x48,0x30,0x48,0x84,0x00, 0x88,0x88,0x50,0x20,0x20,0x00,
  87.  0xf8,0x10,0x20,0x40,0xf8,0x00
  88. };
  89.  
  90.  
  91.  
  92. int BG_MaxX, BG_MaxY;
  93.  
  94.  
  95. void BG_InitFonts(void);
  96.  
  97. #define BG_Z_MIN         10         /* where viewing plane is */
  98. #define BG_Z_MAX         64000         /* where viewing plane is */
  99. #define BG_RADS                  40.7436654  /* pseudo-grads into rads */
  100. #define BG_LOG_FOCUS 8                       /* log perspective foreshortening */
  101. #define G_LINEAR 32                         /* linearely interpolate for */
  102.  
  103. ///////////////////////////////////////////////////////////////////////////
  104. ///////////////////////////////////////////////////////////////////////////
  105. ///////////////////////////////////////////////////////////////////////////
  106.  
  107.  
  108.  
  109. // turn off graphics mode
  110. void BG_TextMode(void)
  111. {
  112. //  GrDestroyContext(w1);
  113.     GrSetMode(GR_default_text);
  114. //  __dpmi_regs r;
  115. //  r.x.ax=0x0003;
  116. //  __dpmi_int(0x10, &r);
  117.  
  118. //delete G_start;              /* polygon's */
  119. //delete G_end;                /* horizontal boundaries */
  120. //delete G_0_start;  /* contains [32-G_P].[G_P] values */
  121. //delete G_0_end;    /* this thingie is to work faster */
  122. //delete G_1_start;  /* then multidimensional array */
  123. //delete G_1_end;    /* hope so, */
  124. //delete G_2_start;
  125. //delete G_2_end;
  126.  
  127.  
  128. }
  129.  
  130. //start graphics mode
  131. void BG_GraphicsMode(char *drv, int a)
  132. {
  133.  
  134.     GrSetDriver(drv);
  135.     if(GrCurrentVideoDriver() == NULL)
  136.     {
  137.         printf("No graphics driver found\n");
  138.         exit(1);
  139.     }
  140.     switch(a)
  141.     {
  142.     case 0: GrSetMode(GR_width_height_color_graphics,320,200,256); break;
  143.     case 1: GrSetMode(GR_width_height_color_graphics,640,350,256); break;
  144.     case 2: GrSetMode(GR_width_height_color_graphics,640,480,256); break;
  145.     case 3: GrSetMode(GR_width_height_color_graphics,800,600,256); break;
  146.     case 4: GrSetMode(GR_width_height_color_graphics,1024,768,256); break;
  147.     exit(1);
  148.     }
  149.     BG_ScreenWidth = GrMaxX()+1;
  150.     BG_ScreenHeight = GrMaxY()+1;
  151.     BG_InitFonts();
  152.     BG_ClipXMin = 0;
  153.     BG_ClipYMin = 0;
  154.     BG_ClipXMax = BG_ScreenWidth-1;
  155.     BG_ClipYMax = BG_ScreenHeight-1;
  156.  
  157.  
  158. }
  159. //load in fonts
  160.  
  161.  
  162. void BG_InitFonts(void)
  163. {
  164.     BG_Font=GrLoadFont("mdl");
  165. //  LDVFont_big=GrLoadFont("helv38b");
  166. //  LDVFont_sym=GrLoadFont("symb16");
  167.     memset(&opt,0,sizeof(opt));
  168.     opt.txo_font   = BG_Font;
  169.     opt.txo_xalign = GR_ALIGN_LEFT;
  170.     opt.txo_yalign = GR_ALIGN_TOP;
  171.     opt.txo_direct    = GR_TEXT_RIGHT;
  172.     opt.txo_fgcolor.v = GrBlack();
  173.     opt.txo_bgcolor.v = 0;
  174.     return;
  175. }
  176.  
  177.  
  178. //draw text
  179. void BG_Text(char *buff, int x, int y, int front, int back)
  180. {
  181.     int l;
  182.     opt.txo_fgcolor.v = front;
  183.     opt.txo_bgcolor.v = back;
  184.     l=strlen(buff);
  185.     GrDrawString(buff,l,x,y,&opt);
  186.     return;
  187. }
  188.  
  189.  
  190. void MDL_Text(word x,word y,char *string,register unsigned char color)
  191. {
  192.   int nm,i,j;
  193.   unsigned char  cur;
  194.   unsigned char  *adr,*l_adr;
  195.  
  196.   adr=G_buffer+BG_ScreenWidth*y+x;
  197.   while(*string!='\0')
  198.   {
  199.     nm=((*string++)-' ');
  200.     if(nm>58) nm-=' ';
  201.     nm*=6;
  202.     for(i=0,l_adr=adr;i<6;i++,l_adr+=BG_ScreenWidth)
  203.     {
  204.       cur=MDL_Font[nm++];
  205.       for(j=0;j<8;j++)
  206.       if(cur&(0x80>>j)) *(l_adr+j)=color;
  207.     }
  208.     adr+=8;                                   /* Next word */
  209.   }
  210. }
  211.  
  212.  
  213.  
  214. void BG_ClearScreen(byte color)
  215. {
  216.     GrClearScreen(color);
  217. }
  218. void BG_OpenPalette(char *palname, unsigned char *pal)
  219. {
  220.     FILE *p;
  221.     int i,j;
  222.     char ch;
  223.  
  224.     if((p=fopen(palname,"rt"))==NULL)
  225.     {
  226.         fprintf(stderr,"can't open %s ... exiting\n",palname);
  227.         exit(1);
  228.     }
  229.  
  230.     while(fgetc(p)!=0x0A); // read some header
  231.     while(fgetc(p)!=0x0A); // smore header
  232.     while(fgetc(p)!=0x0A); // this line is for # colors (better be 256)
  233.  
  234.     for(i=0;i<256;i++)
  235.     {
  236.         fscanf(p,"%d",&j); pal[i*3]=(unsigned char)j;
  237.         fscanf(p,"%d",&j); pal[i*3+1]=(unsigned char)j;
  238.         fscanf(p,"%d",&j); pal[i*3+2]=(unsigned char)j;
  239.  
  240.     }
  241.     fclose(p);
  242.  
  243. }
  244.  
  245. void BG_PutPixel(word x, word y, byte c )
  246. {
  247.     GrPlot(x,y,c);
  248. }
  249.  
  250.  
  251. void BG_Line(int x1, int y1, int x2, int y2, int c)
  252. {
  253.     GrLine(x1,y1,x2,y2,c);
  254. }
  255.  
  256.  
  257. void BG_SetPalette(unsigned char *pal)
  258. {
  259.     int i;
  260.  
  261.     for(i=0;i<256;i++)
  262.     {
  263.         outportb(0x3c8,(byte)i);
  264.         outportb(0x3c9,(byte)pal[i*3]/4);
  265.         outportb(0x3c9,(byte)pal[i*3+1]/4);
  266.         outportb(0x3c9,(byte)pal[i*3+2]/4);
  267.     }
  268.  
  269. }
  270.  
  271.  
  272. struct BMP_HEADER{
  273.  
  274.     unsigned filesize;
  275.     short int reserved1;
  276.     short int reserved2;
  277.     int headersize;
  278.     int infosize;
  279.     int width;
  280.     int height;
  281.     short int planes;
  282.     short int b